#include"stack.h"

void STinit(stack* ps)
{
	assert(ps);
	ps->capacity = ps->top = 0;
	ps->p = NULL;
}

bool STempty(stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

void STpush(stack* ps, STdatetype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STdatetype* tmp = (STdatetype*)realloc(ps->p, sizeof(STdatetype) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->p = tmp;
		ps->capacity = newcapacity;
	}
	ps->p[ps->top] = x;
	ps->top++;
}

void STpop(stack* ps)
{
	assert(ps);
	assert(!STempty(ps));
	ps->top--;
}

int STsize(stack* ps)
{
	assert(ps);
	return ps->top;
}

STdatetype StackTop(stack* ps)
{
	assert(ps);
	assert(!STempty(ps));
	return ps->p[ps->top - 1];
}


void STdestory(stack* ps)
{
	assert(ps);
	free(ps->p);
	ps->top = ps->capacity = 0;
	ps->p = NULL;
}

